﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ECS_Test
{
    public class EntityManager
    {
        public EntityManager()
        {
            allEntities = new Dictionary<int, Entity>();
        }

        public Dictionary<int, Entity> allEntities;

        internal void AddEntity(string entityName)
        {
            Entity e = new Entity();

            e.id = UniqueIdGenerator();

            // Imagine I get the relevant info for creating an entity type from some text file...

            string[] entityTypeData;
            if (entityName == "man")
                entityTypeData = new string[] { "Position", "Physics", "Velocity" };
            else if (entityName == "wall")
                entityTypeData = new string[] { "Position", "Physics" };
            else
                entityTypeData = new string[0];

            // ...therefore the above code would later be compressed to 
            //      string entityTypeData = CheckEntityTypeData(p);

            // Parse the strings into instances of the relevant components:
            List<IComponent> comps = entityTypeData.Select(x => (IComponent)Activator.CreateInstance(Type.GetType("ECS_Test.Comp_" + x))).ToList();

            // Add components to entity's component list:
            foreach (IComponent _ic in comps)
            {
                e.AddComp(_ic);
            }

            // Add entity to each system, which only keeps those that are relevant to it:
            foreach (ISystem _is in GameState.systems)
            {
                _is.Add(entityTypeData, e.id);
            }
            allEntities.Add(e.id, e);
        }

        private int UniqueIdGenerator()
        {
            // Yes, yes, it's rudimentary in that each new entity's id is but the size of the dictionary. Entity removal impossible at the moment.
            return allEntities.Count;
        }
    }
}
